package com.ray.friendslist;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.ray.friendslist.adaptor.FeedCursorAdapter;
import com.ray.friendslist.content.contract.FriendsContract;
import com.ray.friendslist.service.VKFeedService;
import com.ray.friendslist.service.VKFriendsService;

public class VKFeedActivity extends FragmentActivity implements
		LoaderCallbacks<Cursor>, OnItemClickListener {

	private static final String FROM = "FROM";

	private static final String ITEM_ID = "ITEM_ID";

	private ListView mListView;

	private ProgressBar mProgressBar;

	private FeedCursorAdapter mCursorAdapter;

	private BroadcastReceiver mBroadcastReceiver;

	private Cursor mCursor;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_feed);
		Log.w("ray", "Activiti 2 layout created");
		mProgressBar = (ProgressBar) findViewById(R.id.feed_progress);
		mListView = (ListView) findViewById(R.id.feed_list);
		mBroadcastReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				Log.d("onReceive", "");

				String action = intent.getAction();
				if (action.equals(VKFriendsService.ACTION_ON_ERROR)) {
					mProgressBar.setVisibility(View.GONE);
					Toast.makeText(
							VKFeedActivity.this,
							intent.getStringExtra(VKFriendsService.EXTRA_KEY_MESSAGE),
							Toast.LENGTH_SHORT).show();
				} else if (action.equals(VKFriendsService.ACTION_ON_SUCCESS)) {
					mProgressBar.setVisibility(View.GONE);
				}
			}
		};
		IntentFilter filter = new IntentFilter();
		filter.addAction(VKFriendsService.ACTION_ON_ERROR);
		filter.addAction(VKFriendsService.ACTION_ON_SUCCESS);
		registerReceiver(mBroadcastReceiver, filter);
		mCursorAdapter = new FeedCursorAdapter(this, null);
		mListView.setItemsCanFocus(false);
		mListView.setOnItemClickListener(this);
		mListView.setAdapter(mCursorAdapter);
		Log.d("ray", "last");
		getSupportLoaderManager().initLoader(0, null, this);
	}

	public void onStartService(View view) {
		mProgressBar = (ProgressBar) findViewById(R.id.feed_progress);
		mProgressBar.setVisibility(ProgressBar.VISIBLE);
		Intent intent = new Intent(this, VKFeedService.class);
		startService(intent);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}

	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(this,
				FriendsContract.VKFeedColoumns.CONTENT_URI, null, null, null,
				null);
	}

	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		mCursorAdapter.swapCursor(arg1);
	}

	public void onLoaderReset(Loader<Cursor> arg0) {
		mCursorAdapter.swapCursor(null);
	}

	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long arg3) {
		Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
		Intent intent = new Intent(this, VKFeedViewActivity.class);
		String itemID = cursor.getString(cursor
				.getColumnIndex(FriendsContract.VKFeedColoumns.ITEM_ID));
		intent.putExtra(ITEM_ID, itemID);
		intent.putExtra(FROM, "FEEDS");
		startActivity(intent);
	}

}
